{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Download Pre-Trained Global Vectors (GloVe) for Word Representation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We use a dataset that contains 1.6 million training and 350 test tweets from 2009 with algorithmically assigned binary positive and negative sentiment scores that are fairly evenly split."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-21T01:12:58.241678Z",
     "start_time": "2020-06-21T01:12:58.181199Z"
    }
   },
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "import requests\n",
    "from io import BytesIO\n",
    "from zipfile import ZipFile"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Download and unzip"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can learn more about the data and manually download them from [here](https://nlp.stanford.edu/projects/glove/)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-21T01:12:58.256793Z",
     "start_time": "2020-06-21T01:12:58.253117Z"
    }
   },
   "outputs": [],
   "source": [
    "path = Path('glove')\n",
    "if not path.exists():\n",
    "    path.mkdir()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-21T01:12:58.265236Z",
     "start_time": "2020-06-21T01:12:58.258080Z"
    }
   },
   "outputs": [],
   "source": [
    "URLs = ['http://nlp.stanford.edu/data/glove.6B.zip',\n",
    "        'http://nlp.stanford.edu/data/glove.twitter.27B.zip',\n",
    "        'http://nlp.stanford.edu/data/glove.840B.300d.zip']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-21T01:12:58.273139Z",
     "start_time": "2020-06-21T01:12:58.266055Z"
    }
   },
   "outputs": [],
   "source": [
    "targets = [('glove.6B.100d.txt', 'glove.6B.300d.txt'),\n",
    "           ('glove.twitter.27B.200d.txt',),\n",
    "           ('glove.840B.300d.txt',)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-21T01:50:01.394497Z",
     "start_time": "2020-06-21T01:12:58.274059Z"
    }
   },
   "outputs": [],
   "source": [
    "for url, targets in zip(URLs, targets):\n",
    "    response = requests.get(url).content\n",
    "    with ZipFile(BytesIO(response)) as zip_file:\n",
    "        for file in zip_file.namelist():\n",
    "            if file in targets:\n",
    "                local_file = path / target\n",
    "                if not local_file.exists():\n",
    "                    with local_file.open('wb') as output:\n",
    "                        for line in zip_file.open(file).readlines():\n",
    "                            output.write(line)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:ml4t] *",
   "language": "python",
   "name": "conda-env-ml4t-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
